﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SimhoppKjellsFavoriterVT13
{
    [Serializable]
    public class Contest
    {
        public Contest()
        {
        }

        public DateTime date;
        public DateTime deadLine;
        public List<Judge> Judges = new List<Judge>();
        public List<Contestant> Contestants = new List<Contestant>();
        private Contestant currentContestant;
        private int currentRound = 1;
        public string name;
        public int nextID = 0;

        public Contest(String name, DateTime date, DateTime deadLine)
        {
            this.name = name;
            this.date = date;
            this.deadLine = deadLine;
        }

        public void addContestant(string name, DateTime birthDate, Gender g, string nationality, int nextID)
        {
            Contestant temp = new Contestant();

            temp.name = name;
            temp.birthDate = birthDate;
            temp.g = g;
            temp.nationality = nationality;
            temp.ID = nextID;

            Contestants.Add(temp);
            currentContestant = temp;

            this.nextID++;
        }

        public void addJudge(string name, DateTime birthDate, Gender g, string nationality, int nextID)
        {
            Judge temp = new Judge();

            temp.name = name;
            temp.birthDate = birthDate;
            temp.g = g;
            temp.nationality = nationality;
            temp.ID = nextID;

            Judges.Add(temp);

            this.nextID++;
        }

        public void removeContestant(int ID)
        {
            foreach (Contestant c in Contestants)
            {
                if (c.ID == ID)
                {
                    Contestants.Remove(c);
                    return;
                }
            }
        }

        public void removeJudge(int ID)
        {
            foreach (Judge j in Judges)
            {
                if (j.ID == ID)
                {
                    Judges.Remove(j);
                    return;
                }
            }
        }

        public Jump newJump()
        {
            return new Jump();
        }


        public static List<Contestant> calculateScoreBoard(List<Contestant> Contestants)
        {
            List<Contestant> tempList = new List<Contestant>(Contestants);
            List<Contestant> sortedScoreList = new List<Contestant>();

            while (tempList.Count > 0)
            {
                float topScore = -1;
                int highestScoreContestant = 0;
                int now = 0;
                
                foreach (Contestant c in tempList)
                {
                    if (c.getTotalScore() > topScore)
                    {
                        topScore = c.getTotalScore();
                        highestScoreContestant = now;
                        now++;
                    }
                }

                sortedScoreList.Add(tempList[highestScoreContestant]);
                tempList.RemoveAt(highestScoreContestant);
            }
            return sortedScoreList;
        }


        public void randomSortContestants()
        {
            Random randomsort = new Random();
            // Sorts the array "a" of length "length" in random order
            for (int random = 0; random < this.Contestants.Count; random++)
            {
                int selected = random + randomsort.Next(0, this.Contestants.Count - random);
                //swap
                Contestant oldvalue = this.Contestants[random];
                this.Contestants[random] = this.Contestants[selected];
                this.Contestants[selected] = oldvalue;
            }

        }

      
        public void sortedContestants()
        {
            for (int sorted = 0; sorted < Contestants.Count - 1; ++sorted)
            {
                int alphabeticFirst = sorted;
                for (int in_rest = sorted + 1; in_rest < Contestants.Count; ++in_rest)
                {
                    if (string.Compare(Contestants[in_rest].name, Contestants[alphabeticFirst].name) == -1)
                    {
                        alphabeticFirst = in_rest;
                    }
                }
                Contestant temp = Contestants[sorted];
                Contestants[sorted] = Contestants[alphabeticFirst];
                Contestants[alphabeticFirst] = temp;
            }

        }

        public Person getPersonByID(int ID)
        {
            foreach (Person j in Contestants)
            {
                if (j.ID == ID)
                    return j;
            }
            foreach (Person j in Judges)
            {
                if (j.ID == ID)
                    return j;
            }
            return null;


        }

        public void done()
        {
            
        }


    }
}
